function [C] = Ddavid_get_sampling_rate_C_by_MLkNN(X, Y, ValidRate)

M = size(X, 2);

Data = [X Y];
PosData = Data(Y == 1, :);
NegData = Data(Y == -1, :);
SizePos = size(PosData, 1);
SizeNeg = size(NegData, 1);
if(SizePos == 0)
    C = 0;
else
    SizeVPos = ceil(SizePos * ValidRate);
    SizeVNeg = floor(SizeNeg * ValidRate);
    VPosData = PosData(1:SizeVPos, :);
    TrainingData = [PosData((SizeVPos + 1):end, :); NegData((SizeVNeg + 1):end, :)];
    
    VPosX = VPosData(:, 1:M);
    VPosY = VPosData(:, (M + 1));    
    TrainingX = TrainingData(:, 1:M);
    TrainingY = TrainingData(:, (M + 1));
    
    [Prior, PriorN, Cond, CondN] = MLKNN_train(TrainingX, TrainingY', 10, 1);
    [~, ~, ~, ~, ~, Outputs, ~] = MLKNN_test(TrainingX, TrainingY', VPosX, VPosY', 10, Prior, PriorN, Cond, CondN);

    Outputs = Outputs';
    C = sum(Outputs, 1) / size(Outputs, 1);
    
    if(C == 0)
        C = 1;
    end
end
